package cc.rengu.redp.flowable.service.impl;

import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.domain.RedpConstant;
import cc.rengu.redp.common.utils.SortUtil;
import cc.rengu.redp.flowable.dao.FlowDesignMapper;
import cc.rengu.redp.flowable.domain.FlowDesign;
import cc.rengu.redp.flowable.service.FlowDesignService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xushitang
 */
@Service
public class FlowDesignServiceImpl extends ServiceImpl<FlowDesignMapper, FlowDesign> implements FlowDesignService {
    @Override
    public IPage<FlowDesign> flowDesignQuery(FlowDesign flowDesign, QueryRequest request) {
        try {
            Page<FlowDesign> page = new Page<>();
            SortUtil.handlePageSort(request, page, null, RedpConstant.ORDER_DESC, false);
            LambdaQueryWrapper<FlowDesign> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.orderByDesc(FlowDesign::getModifyTime);
            return this.page(page, queryWrapper);
        } catch (Exception e) {
            log.error("查询异常", e);
            return null;
        }
    }

    @Override
    public FlowDesign getOneByModelKey(String modelKey) {
        LambdaQueryWrapper<FlowDesign> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FlowDesign::getModelKey, modelKey);
        List<FlowDesign> list = this.list(lqw);
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }
}
